Imports System.IO
Public Class FormFamilies

    Dim dView As DataView
    Dim myFamily As DataTable
    Private Sub initalizeData()
        Try
            loadData()
        Catch ex As Exception
            Throw ex
        End Try
    End Sub
    Public Sub loadData()
        'Get Data From Database
        Try
            myFamily = Families.GetFamily
            dgvFamilies.DataSource = myFamily
        Catch ex As Exception

        End Try
    End Sub

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        Dbinterface.OpenConnection()
        initalizeData()
    End Sub


    Private Sub FormFamilies_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        UI.FormatDataGrid(dgvFamilies)
        btnAdd.Enabled = App.IsAuthorized(Permissions.Manage_Family_Add)
        btnUpdate.Enabled = App.IsAuthorized(Permissions.Manage_Family_Edit)
        btnDelete.Enabled = App.IsAuthorized(Permissions.Manage_Family_Delete)
    End Sub

    Public Sub UpdateFamily()
        'pass values from DataGridView to FormEmployeeAED when click Button Detail
        Try


            Dim row = Me.dgvFamilies.CurrentRow

            With FormFamilyAED

                If row.Cells("FAMILY_ID").Value Is Nothing Then
                    Exit Sub
                Else
                    .fId = row.Cells("FAMILY_ID").Value
                    .cboEmp_Name.Text = row.Cells("EMPLOYEE_NAME").Value
                    .txtRelative.Text = row.Cells("RELATIVE_NAME").Value
                    .txtDOB.Text = row.Cells("DOB").Value
                    .txtOccupation.Text = row.Cells("OCCUPATION").Value
                    .txtNote.Text = row.Cells("NOTE").Value
                    .txtPhone.Text = row.Cells("PHONE").Value
                    .txtAddress.Text = row.Cells("ADDRESS").Value
                    .txtRelativeType.Text = row.Cells("RELATIVE_TYPE").Value
                    .txtEmail.Text = row.Cells("EMAIL").Value

                    .ShowDialog()
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message)
        End Try
    End Sub

    Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
        'Get last DataGridView Rows when click button add
        Dim i = dgvFamilies.Rows.Count.ToString
        i = i - 1
        dgvFamilies.CurrentCell = dgvFamilies.Rows(i).Cells(1)
        Dim frm As New FormFamilyAED

        frm.ShowDialog()
    End Sub

    Private Sub btnUpdate_Click(sender As Object, e As EventArgs) Handles btnUpdate.Click
        UpdateFamily()
    End Sub

    Public Sub searchDataGridView()
        'Allow user to search data in DataGridView by Name or Phone
        'CmdText.Parameters("@S") = "%" & txtSearch.Text & "%"
        Try
            Dim strFilter As String = "NAME   Like  '%" & txtSearch.Text & "%'  Or RELATIVE_NAME Like  '%" & txtSearch.Text & "%' And ACTIVE = 1"

            dView = New DataView(myFamily, strFilter, "", DataViewRowState.CurrentRows)

            dgvFamilies.DataSource = dView


        Catch ex As Exception
            MessageBox.Show(ex.ToString)
        End Try

    End Sub
    Private Sub txtSearch_TextChanged(sender As Object, e As EventArgs) Handles txtSearch.TextChanged
        searchDataGridView()
    End Sub

    Private Sub btnDelete_Click(sender As Object, e As EventArgs) Handles btnDelete.Click
        Try
            'Check database connection
            Dim cn = Dbinterface.Connection
            If cn.State = ConnectionState.Closed Then

                Exit Sub

            End If

        Catch ex As Exception

        End Try
        Try
            FormDelete.ShowDialog()
            If FormDelete.DialogResult = Windows.Forms.DialogResult.Cancel Then
                Exit Sub
            End If


            'Remove DataGridView Row and Delete data in Database
            For Each row As DataGridViewRow In dgvFamilies.SelectedRows
                If row.Cells("FAMILY_ID").Value = Nothing Then
                    Exit Sub
                End If
                Dim fm As New FamilyInfo
                fm.FamilyId = row.Cells("FAMILY_ID").Value

                Families.DeleteFamily(fm)

                Me.dgvFamilies.Rows.Remove(row)
                dgvFamilies.CurrentCell = dgvFamilies.Rows(0).Cells(1)


            Next


        Catch ex As Exception

        End Try

    End Sub

    Private Sub dgvFamilies_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles dgvFamilies.CellContentClick

    End Sub
End Class